<?php

class CommunityController extends Zend_Controller_Action
{

    public function init()
    {
        // 用户身份
        $this->auth = Zend_Auth::getInstance();
        if ($this->auth->hasIdentity()) {
            $this->view->identity = $this->auth->getIdentity();
        }
        
        $this->_helper->layout->setLayout('community');
    }

    public function indexAction()
    {
        // 循环出分类
        $modelCategory = new Ms_Model_ForumCategory();
        $modelForum = new Ms_Model_Forum();
        $categories = $modelCategory->getCategories()->toArray();
        
        // 重新组装分类和版块数组
        $i = 0;
        foreach ($categories as $key){
            //分类下板块
            $forums = $modelForum->getForums($key['id'])->toArray();
            $categories[$i]['forum'] = $forums;
            $i+=1;
        }
        $this->view->forums = $categories;
    }

    public function forumAction()
    {
        // 主题列表
        $fid = $this->_request->getParam('id');
        
        // 获取分页适配器
        $modelTopic = new Ms_Model_ForumTopic();
        $topics = $modelTopic->getTopics($fid);
        $paginator = new Zend_Paginator($topics);
        // 每页显示主题数
        $paginator->setItemCountPerPage(20);
        // 获得当前显示的页码
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        // 渲染到视图
        $this->view->topics = $paginator;
        $this->view->fid = $fid;
    }

    public function topicAction()
    {
        // 主题
        $id = $this->_request->getParam('id');
        $modelForumTopic = new Ms_Model_ForumTopic();
        $topic = $modelForumTopic->getTopic($id);
        $this->view->topic = $topic;
        
        // 该主题下的帖子
        $modelPost = new Ms_Model_ForumPost();
        $posts = $modelPost->getPosts($id);
        $paginator = new Zend_Paginator($posts);
        $perPage = 20;// 每页显示数据条数
        $paginator->setItemCountPerPage($perPage);
        // 获得当前显示的页码
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $paginator->tid = $id;
        $this->view->posts = $paginator;
        
        // 主题查看数
        $view = $modelForumTopic->viewTopic($id);
        
        // 主题作者
        $modelUser = new Ms_Model_User();
        $uid = $topic['user_id'];
        $user = $modelUser->getUser($uid);
        $this->view->user = $user;
        
        if ($this->auth->hasIdentity()) {
               // 快速回复
            $formPost = new Ms_Form_Comment();
            $pageCount = $paginator->getItemCount($paginator); // 总页数
            $currentPostCount = $paginator->getCurrentItemCount();// 当前页的帖子数目
            if ($currentPostCount < $perPage){
                $formPost->setAction('/community/create-post/tid/'.$id.'/page/'.$pageCount);
            }
            else {
                $pageCount++;
                $formPost->setAction('/community/create-post/tid/'.$id.'/page/'.$pageCount);
            }
            
            $this->view->comment = $formPost;
        }
        else{
            $this->view->comment = null;
        }
    }

    public function createTopicAction()
    {
        $forum_id = $this->_request->getParam('fid');
        $formForum = new Ms_Form_Forum();
        $formForum->removeElement('comment');
        $formForum->removeElement('label');

        if ($this->auth->hasIdentity()) {
               $user_id = $this->auth->getIdentity()->id; // 获取用户id
        }
        else{
               $this->_redirect('/user/login');
        }
        if($this->getRequest()->isPost()){
            if($formForum->isValid($_POST)){
                $subject = $formForum->getValue('subject');
                $post = $formForum->getValue('post');
                $modelForum = new Ms_Model_ForumTopic();
                $newTopic = $modelForum->createTopic($user_id, $forum_id, $subject, $post);
                if ($newTopic != null){
                    $this->_redirect('/community/forum/id/'.$forum_id);
                }
                else {
                    throw new Zend_Exception('发表主题出错！');
                }
            }
        }
        $this->view->forum = $formForum;
    }
    
    public function createPostAction()
    {
        $topic_id = $this->_request->getParam('tid');
        $page_id = $this->_request->getParam('page');// 获取url参数
        $formForum = new Ms_Form_Forum();
        $formForum->removeElement('subject');
        $formForum->removeElement('post');
        if ($this->auth->hasIdentity()) {
            $user_id = $this->auth->getIdentity()->id; // 获取用户id
            if ($this->getRequest()->isPost()){
                if ($formForum->isValid($_POST)){
                    $modelPost = new Ms_Model_ForumPost();
                       $post = $formForum->getValue('comment');
                       $newPost = $modelPost->createPost($topic_id, $user_id, $post);
                       if ($newPost != null){
                           $this->_redirect('/community/topic/id/'.$topic_id.'/page/'.$page_id);
                    }
                    else{
                        throw new Zend_Exception('发表帖子出错！');
                    }
                }
            }
            $this->view->post = $formForum;
        }
        else{
            $this->_redirect('/user/login');
        }
    }

    public function updateTopicAction()
    {
        $id = $this->_request->getParam('id');
        
        $modelForumTopic = new Ms_Model_ForumTopic();
        $currentTopic = $modelForumTopic ->getTopic($id);
        if($this->auth->hasIdentity()){
            if($this->auth->getIdentity()->id == $currentTopic->user_id){
                $formForumTopic = new Ms_Form_Forum();
                $formForumTopic ->removeElement('comment');
                if($this->getRequest()->isPost()){
                    if($formForumTopic->isValid($_POST)){
                        $values = $formForumTopic->getValues();
                        $updateForumTopic = $modelForumTopic->updateTopic($id,$values);
                        if($updateForumTopic == TRUE){
                            $this->_redirect('/community/topic/id/'.$id);
                        } else{
                            throw new Zend_Exception("主题更新失败!");
                        }
                    }
                }
                $formForumTopic ->populate($currentTopic->toArray());
                $this->view->community = $formForumTopic;
            } else{
                throw new Zend_Exception("你不是当前主题的作者，不能对其进行编辑");
            }
        }
        else{
            $this->_redirect('/user/login');
        }
    }
    
    public function updatePostAction()
    {
        $id = $this->_request->getParam('id');
        
        $modelForumPost = new Ms_Model_ForumPost();
        $currentPost = $modelForumPost ->getPost($id);
        if($this->auth->hasIdentity()){
            if($this->auth->getIdentity()->id == $currentPost->user_id or $this->auth->getIdentity()->role == 'admin'){
                $formForumPost = new Ms_Form_Forum();
                $formForumPost->removeElement('subject');
                $formForumPost->removeElement('comment');
                if($this->getRequest()->isPost()){
                    if($formForumPost->isValid($_POST)){
                        $values= $formForumPost->getValues();
                        $updateForumPost = $modelForumPost->updatePost($id,$values);
                        if($updateForumPost == TRUE){
                            $this->_redirect('/community/topic/id/'.$currentPost->topic_id);
                        }
                        else{
                            throw new Zend_Exception("帖子更新失败");
                        }
                    }
                }
                $formForumPost ->populate($currentPost->toArray());
                $this->view->community = $formForumPost;
            } else{
                throw new Zend_Exception("你不是当前帖子的作者，不能对其进行编辑");
            }
        }
        else{
            $this->_redirect('/user/login');
        }
    }
    
    public function deleteTopicAction()
    {
        $id = $this->_request->getParam('id');
        $conf = $this->_request->getParam('conf');
        
        if ($conf == 'yes'){
            $modelForumTopic = new Ms_Model_ForumTopic();
            $forumId = $modelForumTopic->getTopic($id)->forum_id;
            $modelForumTopic->deleteTopic($id);
            $this->_redirect('/community/forum/id/'.$forumId);
        }
        
        $this->view->id = $id;
    }
    
    public function deletePostAction()
    {
        $id = $this->_request->getParam('id');
        $modelForumPost = new Ms_Model_ForumPost();
        $topicId = $modelForumPost ->getPost($id)->topic_id;
        $modelForumPost->deletePost($id);
        $this->_redirect('/community/topic/id/'.$topicId);
    }

}

